package com.phrp.service.impl;

import com.phrp.dao.UserDao;
import com.phrp.entity.User;
import com.phrp.entity.vo.PageData;
import com.phrp.entity.vo.UserSearchDataVo;
import com.phrp.exception.CustomizationException;
import com.phrp.util.returnResult.ReturnInfoEnum;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.phrp.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.phrp.util.returnResult.ResponseResult;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ssc
 * @since 2024-04-06
 */
@Service
public class UserserviceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Autowired
    private UserDao userDao;

    /**
     * 新增用户
     * @param user
     * @return
     */
    @Override
    public ResponseResult addUser(User user) {
        user.setUUuid(UUID.randomUUID().toString().replace("-",""));

        // 存储初始密码
        Md5Hash md5Hash = new Md5Hash("123456",user.getPhone(),1024);

        user.setPassword(md5Hash.toHex());
        user.setUpdateTime(LocalDateTime.now());
        user.setCreateTime(LocalDateTime.now());

        int insert = userDao.insert(user);

        if(insert != 1){
            throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
        }

        return new ResponseResult(ReturnInfoEnum.SUCCESS);
    }

    /**
     * 搜索类
     * @param searchData
     * @return
     */
    @Override
    public ResponseResult searchUser(UserSearchDataVo searchData) {
        searchData.setStartPage((searchData.getCurrentPage()-1)*searchData.getPageSize());
        LinkedList<User> list = userDao.selectSearchPage(searchData);
        Integer totalCount = userDao.selectTotalCount(searchData);

        PageData<List<User>> userPageData = new PageData<>();
        userPageData.setData(list);
        userPageData.setTotalNumber(totalCount);

        return new ResponseResult(ReturnInfoEnum.SUCCESS,userPageData);
    }

    /**
     * 删除用户信息
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = CustomizationException.class)
    @Override
    public ResponseResult deleteUser(LinkedList<String> ids) {
        int i = userDao.deleteBatchIds(ids);

        if(i<=0){
            throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
        }
        return new ResponseResult(ReturnInfoEnum.SUCCESS);
    }

    /**
     * 编辑用户信息
     * @param user
     * @return
     */
    @Override
    public ResponseResult editUser(User user) {
        UpdateWrapper<User> update = new UpdateWrapper();
        update.eq("u_uuid",user.getUUuid())
                .set("real_name",user.getRealName())
                .set("sex",user.getSex())
                .set("age",user.getAge())
                .set("phone",user.getPhone())
                .set("identity",user.getIdentity())
                .set("type",user.getType());

        int i = userDao.update(null, update);
        if( i != 1){
            throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
        }
        return new ResponseResult(ReturnInfoEnum.SUCCESS);
    }

    /**
     * 重置用户密码
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = CustomizationException.class)
    @Override
    public ResponseResult resetPassword(LinkedList<String> ids) {
        // 遍历ID
        for (String id : ids) {
            User user = userDao.selectById(id);
            Md5Hash md5Hash = new Md5Hash("123456",user.getPhone(),1024);
            String password = md5Hash.toHex();
            int i = userDao.updatePwdById(id,password);
            if(i != 1){
                throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
            }
        }

        return new ResponseResult(ReturnInfoEnum.SUCCESS);
    }

    /**
     * 修改用户状态
     * @param id
     * @param type
     * @return
     */
    @Override
    public ResponseResult updateStatus(String id, String type) {
        UpdateWrapper<User> update = new UpdateWrapper<>();
        update.eq("u_uuid",id)
                .set("status",type);

        int i = userDao.update(null, update);

        if(i != 1 ){
            throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
        }

        return new ResponseResult(ReturnInfoEnum.SUCCESS);
    }
}
